
package com.zdkj.subway.rca.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.zdkj.subway.rca.dto.SwCkReportReviewExportDTO;
import com.zdkj.subway.rca.entity.SwCkReportReview;
import com.zdkj.subway.rca.mapper.SysDictCommonMapper;
import com.zdkj.subway.rca.service.base.SwCkReportReviewServiceBase;
import com.zdkj.subway.rca.util.LocalDateConverter;
import com.zdkj.subway.rca.util.LocalDateTimeConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 报表间数据关联审核
 *
 * @author cooze
 * @date 2020-10-27 23:07:56
 */
@Slf4j
@Service
public class SwCkReportReviewServiceImpl extends SwCkReportReviewServiceBase  {


    @Autowired
    private SysDictCommonMapper sysDictCommonMapper;

    @Override
    public void exportExcel(SwCkReportReviewExportDTO dto, HttpServletResponse response) {
        //获取模板excel文件流
        try (InputStream excelTemplate = new PathMatchingResourcePatternResolver().getResource("classpath:/static/excel/template/export/SwCkReportReview.xlsx").getInputStream()) {
            response.reset(); // 非常重要
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", exportFileName());
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerConverter(new LocalDateTimeConverter()).registerConverter(new LocalDateConverter()).withTemplate(excelTemplate).build();
            //填充数据
            List<SwCkReportReview> results = queryExportDataResult(dto);
            excelWriter.fill(results, EasyExcel.writerSheet(excelSheetName()).build());
            excelWriter.finish();
        } catch (Exception e) {
            log.error("SwCkReportReviewServiceImpl export error.", e);
        }
    }

    @Override
    public SysDictCommonMapper getSysDictCommonMapper() {
        return sysDictCommonMapper;
    }
}
